//
//  ResultCircularViewController.m
//  AndCampus
//
//  Created by 史宵宵 on 2018/4/4.
//  Copyright © 2018年 史宵宵. All rights reserved.
//

#import "ResultCircularViewController.h"
#import "UICountingLabel.h"
#import "PNChart.h"

@interface ResultCircularViewController ()<PNChartDelegate>

@end

@implementation ResultCircularViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"测试结果";
    UIImage *image = [UIImage imageNamed:@"Testlist_bg"];
    self.view.layer.contents = (id) image.CGImage;
    self.tableView.frame = CGRectMake(0, 80, kScreenW, kScreenH - 70 - 69- 80);
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    UIView *tableHeadView = [[UIView alloc]init];
//    tableHeadView.backgroundColor = [UIColor yellowColor];
    tableHeadView.frame = CGRectMake(0, 0, kScreenW, 200+150);
    
    self.tableView.tableHeaderView = tableHeadView;
    
    NSArray *items = @[[PNPieChartDataItem dataItemWithValue:[self.perAnswerList[5][@"per"] floatValue]   color:PNInterpersonalColor description:self.perAnswerList[5][@"context"]],
                       [PNPieChartDataItem dataItemWithValue:[self.perAnswerList[0][@"per"] floatValue] color:PNLanguageColor description:self.perAnswerList[0][@"context"]],
                       [PNPieChartDataItem dataItemWithValue:[self.perAnswerList[4][@"per"] floatValue] color:PNMusicColor description:self.perAnswerList[4][@"context"]],
                       [PNPieChartDataItem dataItemWithValue:[self.perAnswerList[2][@"per"] floatValue] color:PNSpaceColor description:self.perAnswerList[2][@"context"]],
                       [PNPieChartDataItem dataItemWithValue:[self.perAnswerList[1][@"per"] floatValue] color:PNLogicColor description:self.perAnswerList[1][@"context"]],
                       [PNPieChartDataItem dataItemWithValue:[self.perAnswerList[3][@"per"] floatValue] color:PNMovementColor description:self.perAnswerList[3][@"context"]],
                       [PNPieChartDataItem dataItemWithValue:[self.perAnswerList[7][@"per"] floatValue] color:PNNatureColor description:self.perAnswerList[7][@"context"]],
                       [PNPieChartDataItem dataItemWithValue:[self.perAnswerList[6][@"per"] floatValue] color:PNIntrospectionColor description:self.perAnswerList[6][@"context"]]];
    
    PNPieChart *pieChart = [[PNPieChart alloc] initWithFrame:CGRectMake((kScreenW - 200)/2, 0, 200, 200) items:items];
//    pieChart.backgroundColor = RGBColor(137, 254, 216);
    pieChart.delegate = self;
    [pieChart strokeChart];
    // 加到父视图上
    [tableHeadView addSubview:pieChart];
    // 显示图例
    pieChart.hasLegend = NO;
    // 横向显示
    pieChart.legendStyle = PNLegendItemStyleStacked;
    // 显示位置
    pieChart.legendPosition = PNLegendPositionTop;
    // 获得图例 当横向排布不下另起一行
    UIView *legend = [pieChart getLegendWithMaxWidth:100];
    legend.frame = CGRectMake((kScreenW - 200)/2, 210, legend.bounds.size.width, legend.bounds.size.height);
    [tableHeadView addSubview:legend];
    
//    tableHeadView.frame = CGRectMake(0, 0, kScreenW, 200+100+legend.bounds.size.height);
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((200- 70)/2, (200- 70)/2, 70, 70)];
    label.backgroundColor = RGBColor(137, 254, 216);
    label.numberOfLines = 0;
    label.text = @"多 元\n智 能";
    label.textColor = [UIColor blackColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:18 weight:500];
//    label.font = [UIFont fontWithName:@"" size:18];
    
//    NSArray *familyNames = [UIFont familyNames];
//    for( NSString *familyName in familyNames ){
//        MyLog(@"Family: %s \n", [familyName UTF8String] );
//        NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
//        for( NSString *fontName in fontNames ){
//            MyLog( @"Font: %s \n", [fontName UTF8String] );
//        }
//    }
    label.clipsToBounds = YES;
    label.layer.cornerRadius = 35;
    [pieChart addSubview:label];
}
- (NSInteger )numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleValue1
                                      reuseIdentifier: CellIdentifier];
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@\n%@",self.answerList[indexPath.row][@"title"],self.answerList[indexPath.row][@"content"]];
    cell.textLabel.font = [UIFont systemFontOfSize:16];
    cell.textLabel.numberOfLines = 0;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [Global getSizeOfString:[NSString stringWithFormat:@"%@\n%@",self.answerList[indexPath.row][@"title"],self.answerList[indexPath.row][@"content"]] maxWidth:kScreenW - 20 maxHeight:MAXFLOAT withFontSize:16.6].height;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectZero;
    view.backgroundColor = KBACKGROUNDCOLOR;
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
